package com.lsx.logisticscore.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsx.logistics.response.CommonResp;
import com.lsx.logisticscore.entity.Employee;
import com.lsx.logisticscore.mapper.EmployeeMapper;
import com.lsx.logisticscore.request.EmployeeVO;
import com.lsx.logisticscore.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    @RequestMapping("/selectByCondition")
    public CommonResp<IPage<Employee>> doGetEmployee( Page<Employee> page,  EmployeeVO vo){
        log.info("====进入selectByCondition===={}, {}, vo:{}", page.getSize(), page.getCurrent(), vo);
        IPage<Employee> content = employeeService.getEmployee(page, vo);
        CommonResp<IPage<Employee>> resp = new CommonResp<>();
        resp.setContent(content);
        return resp;
    }

    @RequestMapping("/{empId}")
    public CommonResp<Employee> doGetEmployeeById(@PathVariable String empId){
        return new CommonResp<Employee>(employeeService.getEmployeeById(empId));
    }

    @RequestMapping("/selectCountByType")
    public CommonResp<List<Map<String, String>>> getEmployeeCountByType(){
        return new CommonResp<>(employeeService.getEmpCountByType());
    }

    @RequestMapping("/update")
    public CommonResp<Integer> updateEmployeeById(@RequestBody Employee employee){
        log.info("=========update======{}", employee);
        Integer result = employeeService.updateEmployee(employee);
        CommonResp<Integer> resp = new CommonResp<>();
        if (result != 1){
            resp.setMessage("更新失败");
            resp.setStatus("-1");
        }else {
            resp.setContent(result);
        }
        return resp;
    }

    @PostMapping("/delete")
    public CommonResp<Integer> deleteEmployeeById(@RequestBody JSONObject empId){
        CommonResp<Integer> resp = new CommonResp<>();

        if (ObjectUtils.isEmpty(empId)) {
            resp.setMessage("工号为空");
            resp.setStatus("-2");
        }
        String id = empId.getString("empId");
        Integer result = employeeService.deleteEmployeeById(id);

        if (result != 1){
            resp.setMessage("删除失败");
            resp.setStatus("-1");
        }else {
            resp.setContent(result);
        }
        return resp;
    }

    @RequestMapping("/insert")
    public CommonResp<Integer> insertEmployee(@RequestBody Employee employee){
        //返回值要做判断的
        Integer result = employeeService.insertEmployee(employee);
        CommonResp<Integer> resp = new CommonResp<>();
        if (result != 1){
            resp.setMessage("插入失败");
            resp.setStatus("-1");
        }else {
            resp.setContent(result);
        }
        return resp;

    }


}
